package kr.hkit.wordnote.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.wordnote2.R;

public class SetUpActivity extends Activity {
	SharedPreferences pref;
	EditText txt1;
	EditText txt2;
	int ckId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setupactivity);
		
		txt1 = (EditText)findViewById(R.id.text_count);
		txt2 = (EditText)findViewById(R.id.text_time);
		
		pref = getSharedPreferences("setup", 0);
		int wordCount = pref.getInt("wordCout", 10);
		int delayTime = pref.getInt("delayTime", 2);
		ckId = pref.getInt("ckId", R.id.savelast);
		txt1.setText(wordCount+"");
		txt2.setText(delayTime+"");
		RadioGroup rg = (RadioGroup)findViewById(R.id.radiogroup);
		rg.check(ckId);
		
		rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				ckId = checkedId;
			}
		});
	}
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.btn_save:
			EditText txt1 = (EditText)findViewById(R.id.text_count);
			EditText txt2 = (EditText)findViewById(R.id.text_time);
			try{
				int wordCount = Integer.parseInt(txt1.getText().toString());
				int delayTime = Integer.parseInt(txt2.getText().toString());
				RadioGroup rg = (RadioGroup)findViewById(R.id.radiogroup);
				pref = getSharedPreferences("setup", 0);
				SharedPreferences.Editor edit = pref.edit();
				edit.putInt("wordCout", wordCount);
				edit.putInt("delayTime", delayTime);
				edit.putInt("ckId", ckId);
				edit.commit();
			
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				finish();
			}catch(Exception e) {
				Toast.makeText(SetUpActivity.this, "숫자를 입력하세요", 0).show();
			}
			break;
		case R.id.btn_back:
			finish();
			break;
		}
	}
}
